/**
 *
 * @author javi
 */

package fmap.gui.help;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.util.Vector;


public class TreeModelImpl implements TreeModel
{
    private Vector treeModelListeners = new Vector();
    private NodeTree root;
    
    public TreeModelImpl(NodeTree _root)
    {
        root = _root;
    }
    
    
//////////////// Fire events //////////////////////////////////////////////
    
    /**
     * The only event raised by this model is TreeStructureChanged with the
     * root as path, i.e. the whole tree has changed.
     */
/*
    protected void fireTreeStructureChanged(Person oldRoot) {
        int len = treeModelListeners.size();
        TreeModelEvent e = new TreeModelEvent(this,
                                              new Object[] {oldRoot});
        for (int i = 0; i < len; i++) {
            ((TreeModelListener)treeModelListeners.elementAt(i)).
                    treeStructureChanged(e);
        }
    }
*/
    
    
    
    protected Vector getChildren(Object node)
    {

        NodeTree n = (NodeTree)node;
        
        Vector v = n.getVSubNodes ();
        
        return v;
    }
    
    
//////////////// TreeModel interface implementation ///////////////////////
    
    /**
     * Adds a listener for the TreeModelEvent posted after the tree changes.
     */
    public void addTreeModelListener(TreeModelListener l)
    {
        treeModelListeners.addElement(l);
    }
    

    /**
     * Returns the child of parent at index index in the parent's child array.
     */
    public Object getChild(Object parent, int index)
    {
        return getChildren(parent).get(index);
    }
    
    /**
     * Returns the number of children of parent.
     */
    public int getChildCount(Object parent)
    {
        Vector children = getChildren(parent);
        return (children == null) ? 0 : children.size();
    }
    
    /**
     * Returns the index of child in parent.
     */

    public int getIndexOfChild(Object parent, Object child)
    {
    /*
        Person p = (Person)parent;
        if (showAncestors)
        {
            int count = 0;
            Person father = p.getFather();
            if (father != null)
            {
                count++;
                if (father == child)
                {
                    return 0;
                }
            }
            if (p.getMother() != child)
            {
                return count;
            }
            return -1;
        }
        return p.getIndexOfChild((Person)child);
    */
        return 1;
//        if ( child instanceof fmap.model.Node )
//        {
//            Node n = (Node)child;
//            return n.getIdNode ();
//        }
        
//        return (super.getChildCount());
    }

    /**
     * Returns the root of the tree.
     */
    public Object getRoot()
    {
        return root;
    }
    
    /**
     * Returns true if node is a leaf.
     */
    public boolean isLeaf(Object node)
    {
        NodeTree n = (NodeTree)node;
        
        if( n.getVSubNodes ().size () > 0 ) return false;
        else                                return true;
    }
    
    /**
     * Removes a listener previously added with addTreeModelListener().
     */
    public void removeTreeModelListener(TreeModelListener l)
    {
        treeModelListeners.removeElement(l);
    }
    
    /**
     * Messaged when the user has altered the value for the item
     * identified by path to newValue.  Not used by this model.
     */
    public void valueForPathChanged(TreePath path, Object newValue)
    {
        System.out.println("*** valueForPathChanged : "
                + path + " --> " + newValue);
    }
}
